package com.archermind.boc.dal.business.service;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.archermind.boc.Logger;
import com.archermind.boc.dal.mode.HibernateSessionFactory;
import com.archermind.boc.domain.Biz;

public class BizService {
	private Logger log = Logger.getLogger(BizService.class.getName());

	public boolean bizAdded(Biz biz) {
		boolean state = true;
		Transaction tx = null;
		Session session = null;
		try {
			session = HibernateSessionFactory.getSession();
			tx = session.beginTransaction();
			session.saveOrUpdate(biz);
			System.out.println(biz.getBizid());
			tx.commit();
		} catch (HibernateException e) {
			state = false;
			tx.rollback();
			log.error("err: save is fail !", e);
		} finally {
			HibernateSessionFactory.closeSession(session);
		}
		return state;
	}

	public boolean modifyBiz(Biz biz) {
		boolean state = true;
		Transaction tx = null;
		Session session = null;
		try {
			session = HibernateSessionFactory.getSession();
			tx = session.beginTransaction();
			session.update(biz);
			tx.commit();
		} catch (HibernateException e) {
			state = false;
			tx.rollback();
			log.error("err: update is fail !", e);
		} finally {
			HibernateSessionFactory.closeSession(session);
		}
		return state;
	}

	public Biz getBizById(int id) {
		Transaction tx = null;
		Session session = null;
		Biz biz = null;
		try {
			session = HibernateSessionFactory.getSession();
			tx = session.beginTransaction();
			biz = (Biz) session.get(Biz.class, id);
			tx.commit();
		} catch (HibernateException e) {
			tx.rollback();
			log.error("err: save is fail !", e);
		} finally {
			HibernateSessionFactory.closeSession(session);
		}
		return biz;
	}

	
	
}
